package com.xxxx.microservice.forestry.config;

import com.xxxx.microservice.forestry.utils.ResponseCode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Swagger API文档配置类
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    @Bean(value = "defaultApi")
    public Docket restfulApi() {
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        Arrays.stream(ResponseCode.values()).forEach(enumRC -> responseMessageList.add(
                new ResponseMessageBuilder().code(enumRC.getCode()).message(enumRC.getMsg()).responseModel(
                        new ModelRef(enumRC.getMsg())
                ).build()
        ));

        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxxx.microservice.forestry.controller")) // 这里指定Controller扫描包路径
                .paths(PathSelectors.regex("(?!/error.*).*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact("yuzq", "", ""))
                .title("林业三普数据库系统接口文档")
                .description("林业三普数据库系统接口文档")
                .version("1.0")
                .build();
    }
}

